package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.institution.service.ICstBscInfoInsCzbankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 客戶资管信息 --浙商銀行 控制层
 */
@Controller
@RequestMapping("/institution/cstCzbank")
public class CstBscInfoInsCzbankController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CstBscInfoInsCzbankController.class);

    @Autowired
    private ICstBscInfoInsCzbankService cstBscInfoInsCzbankService;

    @RequestMapping(value = "/getChangeBankCardSMSCode.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object getChangeBankCardSMSCode(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "成功!");
        String cstId = request.getParameter("cstId"); // 用户id
        String bankId = request.getParameter("bankId"); // 銀行卡ID
        if (StringUtils.isEmpty(cstId)) {
            BaseLogUtils.info(logger,"getChangeBankCardSMSCode","客户ID不能为空",CurrentThreadContext.getCurrentUserName());
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户ID未空");
        }
        if (StringUtils.isEmpty(bankId)) {
            BaseLogUtils.info(logger,"getChangeBankCardSMSCode","银行ID不能为空 cstId="+cstId,CurrentThreadContext.getCurrentUserName());
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "银行ID未空");
        }
        try {
          result = this.cstBscInfoInsCzbankService.getChangeBankCardSMSCode(cstId,bankId);
        } catch (Exception e) {
            BaseLogUtils.info(logger, "getChangeBankCardSMSCode", "获取验证码异常Error："+e.getMessage(), e);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "获取验证码异常");
        }
        return result;
    }

    /**
     * 短信验证码 进行换绑卡
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/changeCardBySMSCode.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object changeCardBySMSCode(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "成功!");
        String cstId = request.getParameter("cstId"); // 用户id
        String bankId = request.getParameter("bankId"); // 銀行卡ID
        String smCode = request.getParameter("smCode"); // 銀行卡ID
        if (StringUtils.isEmpty(cstId)) {
            BaseLogUtils.info(logger,"changeCardBySMSCode","客户ID不能为空",CurrentThreadContext.getCurrentUserName());
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户ID未空");
        }
        if (StringUtils.isEmpty(bankId)) {
            BaseLogUtils.info(logger,"changeCardBySMSCode","银行ID不能为空 cstId="+cstId,CurrentThreadContext.getCurrentUserName());
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "银行ID未空");
        }
        if (StringUtils.isEmpty(smCode)) {
            BaseLogUtils.info(logger,"changeCardBySMSCode","短信验证码不能为空 bankId="+bankId,CurrentThreadContext.getCurrentUserName());
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "短信验证码未空");
        }
        try {
          result = this.cstBscInfoInsCzbankService.changeCardBySMSCode(cstId,bankId,smCode);
        } catch (Exception e) {
            BaseLogUtils.info(logger, "changeCardBySMSCode", "获取验证码异常Error："+e.getMessage(), e);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "获取验证码异常");
        }
        return result;
    }

    /**
	 * 到列表页面
	 */
	@RequestMapping(value = "/showSendMsgCodePage.do", method = {RequestMethod.GET, RequestMethod.POST })
	public ModelAndView showSendMsgCodePage(HttpServletRequest request,HttpServletResponse response) {
	    logger.info("加载浙商还绑卡获取短信页面.");
	    String cstId = request.getParameter("cstId");
		String bankId = request.getParameter("bankId");
        ModelAndView view = new ModelAndView("customer/cstbank/insCzbankSendMsgCodeToBindCard");
        if (StringUtils.isEmpty(cstId)) {
             BaseLogUtils.info(logger,"getChangeBankCardSMSCode","客户ID不能为空",CurrentThreadContext.getCurrentUserName());
             view.addObject("errMsg","客户ID不能为空");
         }
        if (StringUtils.isEmpty(bankId)) {
            BaseLogUtils.info(logger,"getChangeBankCardSMSCode","银行ID不能为空",CurrentThreadContext.getCurrentUserName());
            view.addObject("errMsg","银行ID不能为空");
        }
		view.addObject("cstId", cstId);
		view.addObject("bankId", bankId);
		return view;
    }



}
